package com.wy.android.mp.location;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ZoomControls;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.wy.android.mp.R;

public class MyPositionActivity extends MapActivity {
	
	private LinearLayout layout;
	private MapView mapView;
	private ZoomControls controls;
	private LocationManager locationManager;
	private List<Overlay> mapOverlays;      //标志
	private Drawable drawable;
	private MyPositionItemizedOverlay overlay;
	

	  @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.my_position);
	        layout=(LinearLayout) findViewById(R.id.zoomView);
	        mapView=(MapView) findViewById(R.id.mapview);
	        controls=(ZoomControls) mapView.getZoomControls();
	        layout.addView(controls);
	        
	        mapOverlays=mapView.getOverlays();
	        drawable=this.getResources().getDrawable(R.drawable.androidmarker);
	        overlay=new MyPositionItemizedOverlay(drawable);
	        
	        MapController controller=mapView.getController();
	        GeoPoint point=locate(controller);
	        
	        OverlayItem item=new OverlayItem(point, "","");
	        overlay.addOverlay(item);
	        mapOverlays.add(overlay);
	        
	    }

	

	//是否显示路径
	@Override
	protected boolean isRouteDisplayed() {
		
		return false;
	}
	
	//获得当前经纬度信息，通过mapcontroller定位到该点
	private GeoPoint locate(MapController controller){
		
		locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
		String provider=LocationManager.GPS_PROVIDER;
		Location location=locationManager.getLastKnownLocation(provider);
		
		double lat=0.0;    //纬度
		double lng=0.0;    //经度
		if(location!=null){
			lat=location.getLatitude();
			lng=location.getLongitude();
		} else{
			lat=39.91;
			lng=116.40;
		}
		
		GeoPoint geoPoint=new GeoPoint((int)(lat*1E6),(int)(lng*1E6));
		//定位
		controller.animateTo(geoPoint);
		
		return geoPoint;

	}
	
	
	

}
